<?php

/**
 * 获取钩子类
 */
function hook($layer, $name, $method, $vars = array()) {
    if (empty($name)) return null;
    $apiPath = APP_PATH . '*/' . $layer . '/' . ucfirst($name) . ucwords($layer) . '.php';

    $apiList = glob($apiPath);

    if (empty($apiList)) {
        return;
    }
    $appPathStr = strlen(APP_PATH);
    $method = 'get' . $method . ucwords($name);

    $data = array();
    foreach ($apiList as $value) {
        $path = substr($value, $appPathStr, -4);
        $path = str_replace('\\', '/', $path);
        $appName = explode('/', $path);
        $appName = $appName[0];
//        $config = load_config(APP_PATH . $appName . 'config/config.php', false);
//        if (!$config['APP_SYSTEM'] && (!$config['APP_STATE'] || !$config['APP_INSTALL'])) {
//            continue;
//        }
        $class = '\\app\\' . $appName . '\\' . $layer . '\\' . ucfirst($name) . ucwords($layer);
        if (!class_exists($class)) {
            return null;
        }
        $class = target($appName . '/' . $name, $layer);
        if (method_exists($class, $method)) {
            $data[$appName] = call_user_func_array(array($class, $method), $vars);
        }
    }
    return $data;
}